package com.by.controller;



import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Customer;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.Validation;
import com.by.querypage.CustomerQuery;
import com.by.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Api(tags = "客户管理")
@CrossOrigin
public class CustomerController extends BaseController {

    @Autowired
    CustomerService customerService;


    @ApiOperation("查询客户")
    @GetMapping("/customer")
    public ResultBean get(String id) {
        Customer customer = customerService.get(id);
        return success("客户信息",customer);
    }

    @ApiOperation("保存客户")
    @PostMapping("/customer")
    public ResultBean save(@Validated (ISave.class) @RequestBody Customer customer, BindingResult result){
        Validation.validate(result);
        int rs = customerService.save(customer);
        return saveResult(rs);
    }

    @ApiOperation("查询客户列表")
    @PostMapping("/customer/query")
    public ResultBean<PageBean<List<Customer>>> query(@RequestBody CustomerQuery query){
//        PageBean pageBean = customerService.query(query);
        PageBean pageBean = customerService.queryByMap(BeanUtil.beanToMap(query));
        return success("查询成功",pageBean);
    }

    @ApiOperation("删除客户")
    @DeleteMapping("/customer")
    public ResultBean delete(@RequestParam(required = true) int id){
        int rs = customerService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation("修改客户")
    @PutMapping("/customer")
    public ResultBean update(@RequestBody Customer customer){
        int rs = customerService.update(customer);
        return updateResult(rs);
    }
}
